//
//  YCCoreMotion1ViewController.m
//  DemoShake
//
//  Created by 唐可雄 on 16/8/31.
//  Copyright © 2016年 swustyc. All rights reserved.
//

#import "YCCoreMotion1ViewController.h"

#import <CoreMotion/CoreMotion.h>

@interface YCCoreMotion1ViewController ()

/// 动作管理器
@property (strong, nonatomic) CMMotionManager *manager;

@end

@implementation YCCoreMotion1ViewController

#pragma mark - View Controller

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    if (self.manager.isAccelerometerAvailable) {
        // 加速计可用
        [self.manager startAccelerometerUpdates];
    } else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"加速计不可用" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Setter and Getter

- (CMMotionManager *)manager {
    if (_manager == nil) {
        _manager = [[CMMotionManager alloc] init];
    }
    
    return _manager;
}

#pragma mark - IBAction

- (IBAction)clearBtnDidClick:(id)sender {
    self.textView.text = nil;
}

- (IBAction)sampleBtnDidClick:(id)sender {
    CMAcceleration acceleration = self.manager.accelerometerData.acceleration;
    self.textView.text = [self.textView.text stringByAppendingString:[NSString stringWithFormat:@"x:%f y:%f z:%f\n", acceleration.x, acceleration.y, acceleration.z]];
}

@end
